package com.talent.core.protocol.mqtt.codec.encoder;

import com.talent.core.protocol.mqtt.model.fix.TlMqttFixedHead;
import com.talent.core.protocol.mqtt.model.response.TlMqttPubAck;
import com.talent.core.protocol.mqtt.model.variable.TlMqttPubAckVariableHead;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @ClassName：TlConnackDecoder
 * @Author: hszhou
 * @Date: 2024/11/30 15:35
 * @Description: puback的编码
 */
@ChannelHandler.Sharable
public class TlMqttPubAckEncoder extends MessageToByteEncoder<TlMqttPubAck> {

    private static final  Logger logger = LoggerFactory.getLogger(TlMqttPubAckEncoder.class);
    @Override
    protected void encode(ChannelHandlerContext ctx, TlMqttPubAck res, ByteBuf out) throws Exception {

        logger.debug("进入【PUBACK】的编码器编码消息");
        TlMqttFixedHead fixedHead = res.getFixedHead();
        int length = fixedHead.getLength();
        TlMqttPubAckVariableHead variableHead =res.getVariableHead();
        //回复puback消息
        int messageType= fixedHead.getMessageType()<<4;
        //消息类型
        out.writeByte(messageType);
        //剩余长度
        out.writeByte(length);
        out.writeShort(variableHead.getMessageId());

    }
}
